package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.UserInfoMapper;
import cc.rengu.igas.bsps.common.entity.UserInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表接口实现类
 * Created by 王成 on 2018/6/14.
 */
public class UserInfoMapperImpl implements UserInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public int insertBspUserInfo(UserInfo userInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBspUserInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记用户信息表失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public UserInfo selectBspUserInfoByPrimaryKey(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspUserInfoByPrimaryKey");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserId(userId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, userInfo);
        if (0 != iReturnCode) {
            rglog.error("获取用户信息失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        return userInfo;
    }

    @Override
    public int updateBspUserInfoByPrimaryKey(UserInfo userInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspUserInfoByPrimaryKey");
//        userInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新用户<{}>信息表失败,iReturnCode:<{}>", userInfo.getUserId(), iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public int updateBspUserStatusByPrimaryKey(String instId, String userId, String password, String userStatus) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspUserStatusByPrimaryKey");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserId(userId);
//        userInfo.setPassword(password);
        userInfo.setUserStatus(userStatus);
//        userInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新用户信息表失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public int updateBspUserLoginInfoByPrimaryKey(UserInfo userInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspUserLoginInfoByPrimaryKey");
        userInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新用户登录次数信息失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public int updateBspUserTransPasswordByPrimaryKey(String instId, String userId, String password) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsUserTransPasswordByPrimaryKey");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserId(userId);
        userInfo.setTransPassword(password);
        userInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新用户交易密码信息失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public List<UserInfo> selectBspsUserInfoByUserLoginId(String instId, String userLoginId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsUserInfoByUserLoginId");
        UserInfo userInfo = new UserInfo();
        userInfo.setInstId(instId);
        userInfo.setUserLoginId(userLoginId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userInfo, UserInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取用户信息失败,instId:<{}>,userLoginId:<{}>", instId, userLoginId);
            return null;
        }
        return objectList.stream().map(item -> (UserInfo) item).collect(Collectors.toList());
    }
}
